package org.jeecg.modules.activiti.listener;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.zcgl.entity.LeaseContractInfo;
import org.jeecg.modules.zcgl.entity.LeaseContractStandardRent;
import org.jeecg.modules.zcgl.enums.LeaseContractStatusEnum;
import org.jeecg.modules.zcgl.service.ILeaseContractInfoService;
import org.jeecg.modules.zcgl.service.ILeaseContractStandardRentService;
import org.jeecg.modules.zcgl.service.impl.LeaseContractInfoServiceImpl;
import org.jeecg.modules.zcgl.service.impl.LeaseContractStandardRentServiceImpl;
import org.jeecg.modules.zcgl.vo.LeaseContractInfoPage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 合同生效审批后监听
 */
@Component
public class LeaseContractTakeEffectListener implements ExecutionListener {


    private ActBusinessServiceImpl actBusinessService;
    private ILeaseContractInfoService leaseContractInfoService;
    private ILeaseContractStandardRentService leaseContractStandardRentService;

    public void notify(DelegateExecution execution) throws Exception {
        if (leaseContractStandardRentService == null){
            leaseContractStandardRentService = (LeaseContractStandardRentServiceImpl)SpringContextUtils.getBean("leaseContractStandardRentServiceImpl");
        }
        if (leaseContractInfoService == null){
            leaseContractInfoService = (LeaseContractInfoServiceImpl)SpringContextUtils.getBean("leaseContractInfoServiceImpl");
        }
        if (actBusinessService == null){
            actBusinessService = (ActBusinessServiceImpl) SpringContextUtils.getBean("actBusinessServiceImpl");
        }

        String eventName = execution.getEventName();
        //start
        if (EVENTNAME_START.equals(eventName)) {
        } else if (EVENTNAME_END.equals(eventName)) {
            ActBusiness actBusiness = actBusinessService.getById(execution.getProcessBusinessKey());
            if (actBusiness != null && "zcgl_lease_contract_info".equalsIgnoreCase(actBusiness.getTableName())){
                LeaseContractInfo leaseContractInfo = leaseContractInfoService.getById(actBusiness.getTableId());
                List<LeaseContractStandardRent> leaseContractStandardRentList = leaseContractStandardRentService.selectByMainId(leaseContractInfo.getId());
                LeaseContractInfoPage page = new LeaseContractInfoPage();
                BeanUtils.copyProperties(leaseContractInfo, page);
                page.setLeaseContractStandardRentList(leaseContractStandardRentList);
                page.setStatus(LeaseContractStatusEnum.TAKE_EFFECT.getStatus());
                if (page.getSigningDate() == null){
                    page.setSigningDate(DateUtils.getDate());
                }
                leaseContractInfoService.takeEffect(page);
            }
        }
//        System.out.println("----" + execution.getProcessBusinessKey() + "" + execution.getProcessDefinitionId());
    }
}
